<!DOCTYPE html>
<html>
<head>
    <title>Island solver demo - p2.js physics engine</title>
    <script src="../build/p2.js"></script>
    <script src="../build/p2.renderer.js"></script>
    <link href="css/demo.css" rel="stylesheet"/>
    <meta name="description" content="How to use the IslandSolver for improved performance.">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
    <script>

        var interval;

        var N = 10,  // Number of circles in each rope
            M = 10,  // Number of ropes
            r = 0.1; // Circle radius

        // Create demo application
        var app = new p2.WebGLRenderer({

            setup: function(){

                // Create a world with island splitting enabled.
                // The island splitting will cut the scene into independent islands and treat them as separate simulations. This can improve performance.
                var world = new p2.World({
                    gravity : [0,-10],
                    islandSplit : true
                });

                this.setWorld(world);

                world.solver.tolerance = 0.1;
                world.solver.iterations = N;

                // Create circle ropes
                for(var j=0; j<M; j++){
                    var lastBody;
                    for(var i=N; i>=0; i--){
                        var x = (j+0.5-M/2)*r*8;
                        var y = (N/2-i)*r*2.1;
                        var p = new p2.Body({
                            mass: i==0 ? 0 : 1,
                            position: [x, y]
                        });
                        p.addShape(new p2.Circle({ radius: r }));
                        world.addBody(p);
                        if(lastBody){
                            // Connect the current body to the previous one
                            var dist = Math.abs(p.position[1]-lastBody.position[1]);
                            var constraint = new p2.DistanceConstraint(p,lastBody, {
                                distance: dist
                            });
                            world.addConstraint(constraint);
                        } else {
                            p.velocity[0] = 1*i;
                        }
                        lastBody = p;
                    }
                    lastBody = null;
                }

                // Print the number of independent islands to console repeatedly.
                // This will output 10 if the ropes don't touch.
                interval = setInterval(function(){
                    var numIslands = world.unionFind.count;
                    console.log("Number of islands:",numIslands);
                },1000);
            },

            teardown: function(){
                clearInterval(interval);
            }
        });

    </script>
</body>
</html>
